package com.ghost.alibabaghost.service.sys;

import com.ghost.alibabaghost.api.service.sys.SysRoleService;
import com.ghost.alibabaghost.api.model.sys.SysRoleModel;
import com.ghost.alibabaghost.dao.dataobject.sys.SysRoleDO;
import com.ghost.alibabaghost.dao.mapper.sys.SysRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/14 23:14
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public SysRoleModel selectById(Integer id) {
        SysRoleDO sysRoleDO = sysRoleMapper.selectById(id);
        SysRoleModel sysRoleModel = new SysRoleModel();
        BeanUtils.copyProperties(sysRoleDO, sysRoleModel);
        return sysRoleModel;
    }

    @Override
    public List<SysRoleModel> queryAll() {
        return sysRoleMapper.queryAll().stream().map(sysRoleDO -> {
            SysRoleModel sysRoleModel = new SysRoleModel();
            BeanUtils.copyProperties(sysRoleDO, sysRoleModel);
            return sysRoleModel;
        }).collect(Collectors.toList());
    }

    @Override
    public Integer deleteRoleById(Integer id) {
        return sysRoleMapper.deleteById(id);
    }

    @Override
    public Integer saveRole(SysRoleModel sysRoleModel) {
        SysRoleDO sysRoleDO = new SysRoleDO();
        BeanUtils.copyProperties(sysRoleModel, sysRoleDO);
        Integer integer = sysRoleMapper.saveRole(sysRoleDO);
        return integer;
    }

    @Override
    public Integer updateRole(SysRoleModel sysRoleModel) {
        SysRoleDO sysRoleDO = new SysRoleDO();
        BeanUtils.copyProperties(sysRoleModel, sysRoleDO);
        Integer integer = sysRoleMapper.updateRole(sysRoleDO);
        return integer;
    }

    @Override
    public SysRoleModel selectRoleById(Integer id) {
        SysRoleDO sysRoleDo = sysRoleMapper.selectById(id);
        SysRoleModel sysRoleModel = new SysRoleModel();
        BeanUtils.copyProperties(sysRoleDo, sysRoleModel);
        return sysRoleModel;
    }
}
